CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/pages/share/public_paths/[...id].tsx
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { join } from "path";
7
import NextHead from "next/head";
8
9
import basePath from "lib/base-path";
10
import getPublicPathInfo from "lib/share/get-public-path-info";
11
import shareURL from "lib/share/share-url";
12
import withCustomize from "lib/with-customize";
13
import { getPublicPathNames } from "lib/names/public-path";
14
import PublicPath, { PublicPathProps } from "components/path/path";
15
16
import ogShareLogo from "public/logo/og-share-logo.png";
17
18
export default (props: PublicPathProps) => (
19
<>
20
<PublicPath {...props} />
21
<NextHead>
22
<meta property="og:type" content="article"/>
23
<meta property="og:title" content={props.path}/>
24
25
{props.description && (
26
<meta property="og:description" content={props.description}/>
27
)}
28
{props.ogUrl && (
29
<meta property="og:url" content={props.ogUrl}/>
30
)}
31
{props.ogImage && (
32
<meta property="og:image" content={props.ogImage}/>
33
)}
34
{props.created && (
35
<meta property="article:published_time" content={props.created}/>
36
)}
37
{props.last_edited && (
38
<meta property="article:modified_time" content={props.last_edited}/>
39
)}
40
</NextHead>
41
</>
42
);
43
44
export async function getServerSideProps(context) {
45
const id = context.params.id[0];
46
const relativePath = context.params.id.slice(1).join("/");
47
try {
48
const names = await getPublicPathNames(id);
49
if (names != null) {
50
// redirect
51
let location = join(
52
basePath,
53
names.owner,
54
names.project,
55
names.public_path,
56
);
57
if (context.params.id.length > 1) {
58
location = join(
59
location,
60
"files",
61
context.params.id.slice(1).join("/"),
62
);
63
}
64
return { props: { redirect: location } };
65
}
66
const props: PublicPathProps = await getPublicPathInfo({
67
id,
68
relativePath,
69
req: context.req,
70
});
71
72
const customize = await withCustomize({ context, props });
73
74
if (customize?.props?.customize != null) {
75
// Add full URL for social media sharing
76
//
77
customize.props.ogUrl = `${customize.props.customize.siteURL}${shareURL(
78
id,
79
relativePath,
80
)}`;
81
82
// Add image path for social media sharing
83
//
84
customize.props.ogImage = customize.props.customize.logoSquareURL ||
85
`${customize.props.customize.siteURL}${ogShareLogo.src}`;
86
}
87
88
return customize;
89
} catch (_err) {
90
console.log(_err);
91
return { notFound: true };
92
}
93
}
94
95